import { getPayrollData } from '../api/PayrollList'
import { getApplicationData, setApplicationData } from '../api/application'

export default {
  state: {
    peopleData: [],
    length: 0,
    payrollData: [],
    payrollDataTotle: 0,
    applicationData: [],
    applicationDataTotle: 0,
    setPeopleData: []
  },
  reducers: {
    changePeopleData (state, action) {
      return {
        ...state,
        peopleData: action.tableList,
        length: action.length
      }
    },
    filtrateListData (state, action) {
      return {
        ...state,
        peopleData: action.tableList,
        length: action.length
      }
    },
    setPayrollData (state, action) {
      return {
        ...state,
        payrollData: action.data,
        payrollDataTotle: action.total
      }
    },
    setApplicationData (state, action) {
      return {
        ...state,
        applicationData: action.data,
        applicationDataTotle: action.total || state.applicationDataTotle
      }
    },
    updateApplicationData (state, action) {
      let arr = [...state.applicationData]
      let index = state.applicationData.findIndex(item => item.id === action.data.id)
      arr.splice(index, 1, action.data)
      return {
        ...state,
        applicationData: arr,
      }
    },
    initSetPeople (state, action) {
      return {
        ...state,
        setPeopleData: action.data
      }
    },
    addSetpeople (state, action) {
      return {
        ...state,
        setPeopleData: action.data
      }
    },
    delSetpeople (state, action) {
      return {
        ...state,
        setPeopleData: action.data
      }
    },
  },
  effects: {
    *getPayrollData ({ params }, { call, put }) {
      const { data, headers } = yield call(getPayrollData, params)
      yield put({ type: 'setPayrollData', data, total: headers['x-total-count'] })
    },
    *getApplicationData ({ params }, { call, put }) {
      const { data, headers } = yield call(getApplicationData, params)
      yield put({ type: 'setApplicationData', data, total: headers['x-total-count'] })
    },
    *setApplicationDataAsync ({ data }, { call, put }) {
      yield call(setApplicationData, data)
      yield put({ type: 'updateApplicationData', data })
    }
  },
  subscriptions: {}
}
